BasecampAgent use the remote time rather then ours

In the initial version of the BasecampAgent we used our own time to
query for new events. However that fails horrible if the clock of the
huginn server is out of sync the with clock of the api server.
Since we never can keep those clocks in sync we now rely on the
timestamp of the last event send to us.

Dominik Sander 10 years ago
parent
commit
f9342ef335
2 changed files with 29 additions and 27 deletions
  1. 24 23
      app/models/agents/basecamp_agent.rb
  2. 5 4
      spec/models/agents/basecamp_agent_spec.rb

+ 24 - 23
app/models/agents/basecamp_agent.rb

@@ -21,25 +21,25 @@ module Agents
21 21
     event_description <<-MD
22 22
       Events are the raw JSON provided by the Basecamp API. Should look something like:
23 23
 
24
-        {
25
-          "creator": {
26
-            "fullsize_avatar_url": "https://dge9rmgqjs8m1.cloudfront.net/global/dfsdfsdfdsf/original.gif?r=3",
27
-            "avatar_url": "http://dge9rmgqjs8m1.cloudfront.net/global/dfsdfsdfdsf/avatar.gif?r=3",
28
-            "name": "Dominik Sander",
29
-            "id": 123456
30
-          },
31
-          "attachments": [],
32
-          "raw_excerpt": "test test",
33
-          "excerpt": "test test",
34
-          "id": 6454342343,
35
-          "created_at": "2014-04-17T10:25:31.000+02:00",
36
-          "updated_at": "2014-04-17T10:25:31.000+02:00",
37
-          "summary": "commented on whaat",
38
-          "action": "commented on",
39
-          "target": "whaat",
40
-          "url": "https://basecamp.com/12456/api/v1/projects/76454545-explore-basecamp/messages/76454545-whaat.json",
41
-          "html_url": "https://basecamp.com/12456/projects/76454545-explore-basecamp/messages/76454545-whaat#comment_76454545"
42
-        }
24
+          {
25
+            "creator": {
26
+              "fullsize_avatar_url": "https://dge9rmgqjs8m1.cloudfront.net/global/dfsdfsdfdsf/original.gif?r=3",
27
+              "avatar_url": "http://dge9rmgqjs8m1.cloudfront.net/global/dfsdfsdfdsf/avatar.gif?r=3",
28
+              "name": "Dominik Sander",
29
+              "id": 123456
30
+            },
31
+            "attachments": [],
32
+            "raw_excerpt": "test test",
33
+            "excerpt": "test test",
34
+            "id": 6454342343,
35
+            "created_at": "2014-04-17T10:25:31.000+02:00",
36
+            "updated_at": "2014-04-17T10:25:31.000+02:00",
37
+            "summary": "commented on whaat",
38
+            "action": "commented on",
39
+            "target": "whaat",
40
+            "url": "https://basecamp.com/12456/api/v1/projects/76454545-explore-basecamp/messages/76454545-whaat.json",
41
+            "html_url": "https://basecamp.com/12456/projects/76454545-explore-basecamp/messages/76454545-whaat#comment_76454545"
42
+          }
43 43
     MD
44 44
 
45 45
     default_schedule "every_10m"
@@ -61,12 +61,13 @@ module Agents
61 61
     def check
62 62
       service.prepare_request
63 63
       reponse = HTTParty.get request_url, request_options.merge(query_parameters)
64
-      memory[:last_run] = Time.now.utc.iso8601
65
-      if last_check_at != nil
66
-        JSON.parse(reponse.body).each do |event|
64
+      events = JSON.parse(reponse.body)
65
+      if !memory[:last_event].nil?
66
+        events.each do |event|
67 67
           create_event :payload => event
68 68
         end
69 69
       end
70
+      memory[:last_event] = events.first['created_at'] if events.length > 0
70 71
       save!
71 72
     end
72 73
 
@@ -80,7 +81,7 @@ module Agents
80 81
     end
81 82
 
82 83
     def query_parameters
83
-      memory[:last_run].present? ? { :query => {:since => memory[:last_run]} } : {}
84
+      memory[:last_event].present? ? { :query => {:since => memory[:last_event]} } : {}
84 85
     end
85 86
   end
86 87
 end

+ 5 - 4
spec/models/agents/basecamp_agent_spec.rb

@@ -6,7 +6,7 @@ describe Agents::BasecampAgent do
6 6
 
7 7
   before(:each) do
8 8
     stub_request(:get, /json$/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/basecamp.json")), :status => 200, :headers => {"Content-Type" => "text/json"})
9
-    stub_request(:get, /Z$/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/basecamp.json")), :status => 200, :headers => {"Content-Type" => "text/json"})
9
+    stub_request(:get, /02:00$/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/basecamp.json")), :status => 200, :headers => {"Content-Type" => "text/json"})
10 10
     @valid_params = { :project_id => 6789 }
11 11
 
12 12
     @checker = Agents::BasecampAgent.new(:name => "somename", :options => @valid_params)
@@ -43,7 +43,7 @@ describe Agents::BasecampAgent do
43 43
 
44 44
     it "should provide the since attribute after the first run" do
45 45
       time = (Time.now-1.minute).iso8601
46
-      @checker.memory[:last_run] = time
46
+      @checker.memory[:last_event] = time
47 47
       @checker.save
48 48
       @checker.reload.send(:query_parameters).should == {:query => {:since => time}}
49 49
     end
@@ -51,9 +51,10 @@ describe Agents::BasecampAgent do
51 51
   describe "#check" do
52 52
     it "should not emit events on its first run" do
53 53
       expect { @checker.check }.to change { Event.count }.by(0)
54
+      expect(@checker.memory[:last_event]).to eq '2014-04-17T10:25:31.000+02:00'
54 55
     end
55 56
     it "should check that initial run creates an event" do
56
-      @checker.last_check_at = Time.now - 1.minute
57
+      @checker.memory[:last_event] = '2014-04-17T10:25:31.000+02:00'
57 58
       expect { @checker.check }.to change { Event.count }.by(1)
58 59
     end
59 60
   end
@@ -61,7 +62,7 @@ describe Agents::BasecampAgent do
61 62
   describe "#working?" do
62 63
     it "it is working when at least one event was emited" do
63 64
       @checker.should_not be_working
64
-      @checker.last_check_at = Time.now - 1.minute
65
+      @checker.memory[:last_event] = '2014-04-17T10:25:31.000+02:00'
65 66
       @checker.check
66 67
       @checker.reload.should be_working
67 68
     end